<?php
namespace Tbf\Bundle\PackageBundle\TreePackage;
use Gedmo\Exception\UnexpectedValueException;
use Tbf\Bundle\TbfBundle\Controller\TbfController;
use Tbf\Bundle\AjkApiBundle\Annotation\Api;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * 抽象树状api组件
 */
class TreeController extends TbfController
{
    protected $dr_name;
    protected $create_field_list;
    protected $update_field_list;
    protected $entity_class_name;
    public function __construct($config)
    {
        $this->dr_name = $config['dr_name'];
        $this->create_field_list = $config['create_field_list'];
        $this->update_field_list = $config['update_field_list'];
        $this->entity_class_name = $config['entity_class_name'];
        $this->list_field_list = $config['list_field_list'];
        $this->from_array_field_list = $config['from_array_field_list'];
    }
    /**
     * @Route("/list")
     * @Api
     */
    public function listAction()
    {
        $data = $this->getThisDR()->findAll();

        return array($data,null);
    }

    /**
     * @Route("/create")
     * @Api
     */
    public function createAction($input)
    {
        $err = $this->checkParameterExist($input,$this->getCreateFieldNameList());
        if ($err!==null) {
            return array(null,$err);
        }
        $entity = $this->newEntity();
        if ($input['pid']!=0) {
            $parent = $this->getThisDR()->find($input['pid']);
            if ($parent===null) {
                return array(null,'parent not exists');
            }
            $entity->setParent($parent);
        }
        $this->loadFromArray($entity,$input);
        $this->getDM()->persist($entity);
        $this->getDM()->flush();

        return array(null,null);
    }

    /**
     * @Route("/update")
     * @Api
     */
    public function updateAction($input)
    {
        $this->filterParameter($input,$this->getUpdateFieldNameList());
        $err = $this->checkParameterExist($input,array('id'));
        if ($err!==null) {
            return array(null,$err);
        }
        $id = $input['id'];
        $entity = $this->getThisDR()->find($id);
        if ($entity===null) {
            return array(null,'update entity not exists');
        }
        if (!empty($input['pid']) && $input['pid']!=0) {
            $parent = $this->getThisDR()->find($input['pid']);
            if ($parent===null) {
                return array(null,'parent not exists');
            }
            $entity->setParent($parent);
        }
        $this->loadFromArray($entity,$input);
        $this->getDM()->persist($entity);
        try {
            $this->getDM()->flush();
        } catch (UnexpectedValueException $e) {
            return array(null,$e->getMessage());
        }

        return array(null,null);
    }

    /**
     * @Route("/delete")
     * @Api
     */
    public function deleteAction($input)
    {
        $err = $this->checkParameterExist($input,array('id'));
        if ($err!==null) {
            return array(null,$err);
        }
        $id = $input['id'];
        $entity = $this->getThisDR()->find($id);
        if ($entity===null) {
            return array(null,'delete entity not exists');
        }
        $this->getDM()->remove($entity);
        $this->getDM()->flush();

        return array(null,null);
    }

    /**
     * @return \Gedmo\Tree\Entity\Repository\ClosureTreeRepository
     */
    protected function getThisDR()
    {
        return $this->getDR($this->dr_name);
    }

    /**
     * 创建动作的字段名列表
     * @return array
     */
    protected function getCreateFieldNameList()
    {
        return $this->create_field_list;
    }
    /**
     * 更新动作的字段名列表
     * @return array
     */
    protected function getUpdateFieldNameList()
    {
        return $this->update_field_list;
    }
    protected function getListFieldNameList()
    {
        return $this->list_field_list;
    }
    protected function getFromArrayFieldNameList()
    {
        return $this->from_array_field_list;
    }
    /**
     * 新entity工厂
     */
    protected function newEntity()
    {
        $class_name = $this->entity_class_name;
        $class_name = ltrim($class_name,'\\');
        $class_name = '\\'.$class_name;

        return new $class_name();
    }
}
